package cn.wyu.web;

import cn.wyu.pojo.Article;
import cn.wyu.service.ArticleService;
import cn.wyu.service.ArticleServletImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/index.do")
public class IndexServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //处理中文乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //1.接收浏览器发送过来的数据
        String type_code = "0001";
        String type_codeinput = request.getParameter("type_code");
        //判断是否是第一次访问
        if(null != type_codeinput && !"".equals(type_codeinput))
        {
            type_code = type_codeinput;
        }
        System.out.println("要查找的类型是" + type_code);
        //2.将数据交给业务层
        ArticleService is = new ArticleServletImpl();
        List<Article> list = is.findByCodeType(type_code);

        //3.将数据放到request域中
        request.setAttribute("list",list);


        //跳转到index页面
        //转发
        request.getRequestDispatcher("WEB-INF/ArticleIndex.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }
}

